﻿using Base.Common.Events;
using Base.ModuleBase.Interfase;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Windows;
using System.Windows.Input;

namespace Base.ModuleBase
{
    public class ViewModelBase : BindableBase, IDropable
    {
        #region 成员

        /// <summary>
        /// View激活状态改变时发生
        /// </summary>        
        public event EventHandler IsActiveChanged;

        #endregion

        #region 属性

        private bool _isActive;
        /// <summary>
        /// 指示View是否激活
        /// </summary>
        public bool IsActive
        {
            get { return _isActive; }
            set
            {
                _isActive = value;
                OnActive();
            }
        }

        #endregion

        #region 构造
        /// <summary>
        /// 初始化构造
        /// </summary>
        public ViewModelBase()
        {
            OnInit();
        }
        #endregion

        #region 方法
        /// <summary>
        /// 初始化
        /// </summary>
        private void OnInit()
        {
            GlobalManager.EventAggregator.GetEvent<LogoutEvent>().Subscribe((args) =>
            {
                UiDispatcherHelper.Invoke(() => OnLogout(args));
            });

            Init(null);

            BindEvents();
        }

        /// <summary>
        /// 在ViewModel中绑定路由事件的处理程序。
        /// </summary>
        /// <param name="cmdCollection"></param>
        public virtual void BindCommands(CommandBindingCollection cmdCollection)
        {

        }

        /// <summary>
        /// 注册事件
        /// </summary>
        /// <param name="eventAggregator"></param>
        public virtual void BindEvents()
        {

        }

        /// <summary>
        /// 登出
        /// </summary>
        /// <param name="args"></param>
        public virtual void OnLogout(object args)
        {

        }

        /// <summary>
        /// 提供给view调用，子类重写以进行初始化的空方法。
        /// </summary>
        /// <param name="param"></param>
        public virtual void Init(object param) { }

        /// <summary>
        /// 激活
        /// </summary>
        public void OnActive()
        {
            IsActiveChanged?.Invoke(this, null);
        }

        #region 拖拽
        /// <summary>
        /// 鼠标拖拽移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public virtual void PreviewDragOver(object sender, DragEventArgs e) { e.Effects = DragDropEffects.None; }

        /// <summary>
        /// 鼠标拖拽放下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public virtual void Drop(object sender, DragEventArgs e) { e.Effects = DragDropEffects.None; }  
        #endregion
        #endregion
    }
}
